<template>
  <el-dialog width="620px" id="hjDialog" :visible.sync="visible" title="航迹轨迹预测" v-drag>
    <el-form
      ref="predictFormRef"
      :model="predictForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="船舶舷号">
        <el-input v-model="predictForm.hull_name"></el-input>
      </el-form-item>
      <el-form-item label="MMSI">
        <el-input v-model="predictForm.mmsi"></el-input>
      </el-form-item>

      <el-form-item label="当前位置" prop="start_point_lon">
        <el-col :span="11">
          <el-form-item prop="start_point_lon">
            <el-input
              v-model="predictForm.start_point_lon"
              placeholder="经度"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="2">
          <el-form-item prop="start_point_lat">
            <el-input
              v-model="predictForm.start_point_lat"
              placeholder="维度"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="预测位置">
        <el-col :span="11">
          <el-form-item prop="end_point_lon">
            <el-input
              v-model="predictForm.end_point_lon"
              placeholder="经度"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11" :offset="2">
          <el-form-item prop="end_point_lat">
            <el-input
              v-model="predictForm.end_point_lat"
              placeholder="维度"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="预测精度" prop="size">
        <el-select v-model="predictForm.size" placeholder="请选择预测精度">
          <el-option
            v-for="item of sizeOption"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="事件类型">
        <el-select
          v-model="predictForm.event_type"
          placeholder="请选择事件类型"
        >
          <el-option
            v-for="item of typeOption"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="predictSave()" :loading="loading"
        >预测</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import * as request from "../http/request.js";
// 默认表单数据
const baseForm = {
  hull_name: "", //船名
  mmsi: "",
  start_point_lon: null, // 起点经度
  start_point_lat: null, // 起点纬度
  end_point_lon: null, // 终点经度
  end_point_lat: null, //   终点纬度
  event_type: null, //事件类型
  size: 1,
};
const validateLongitude = (rule, value, callback) => {
  //经度,整数部分为0-180小数部分为0到15位
  var longreg =
    /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,15})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,15}|180)$/;
  if (value && !longreg.test(value)) {
    // callback(new Error("经度整数部分为0-180,小数部分为0到15位!"));
    callback(new Error("经度格式不正确!"));
  }
  callback();
};
const validateLatitude = (rule, value, callback) => {
  //纬度,整数部分为0-90小数部分为0到15位
  var latreg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,15}|90\.0{0,15}|[0-8]?\d{1}|90)$/;
  if (value && !latreg.test(value)) {
    callback(new Error("纬度格式不正确!"));
  }
  callback();
};

export default {
  data() {
    return {
      predictForm: { ...baseForm },
      visible: false,
      loading: false,
      rules: {
        start_point_lon: [
          { required: true, message: "请填写当前经度", trigger: "blur" },
          { validator: validateLongitude, trigger: "blur" },
        ],
        start_point_lat: [
          { required: true, message: "请填写当前维度", trigger: "blur" },
          { validator: validateLatitude, trigger: "blur" },
        ],
        end_point_lon: [{ validator: validateLongitude, trigger: "blur" }],
        end_point_lat: [{ validator: validateLongitude, trigger: "blur" }],
        size: [
          { required: true, message: "请选择预测精度", trigger: "change" },
        ],
      },
      typeOption: [
        {
          name: "演习",
          value: "演习",
        },
        {
          name: "挑衅",
          value: "挑衅",
        },
        {
          name: "基地",
          value: "基地",
        },
        {
          name: "补给",
          value: "补给",
        },
        {
          name: "热点区域",
          value: "热点区域",
        }
      ],
      sizeOption: [
        {
          name: 1,
          value: 1,
        },
        {
          name: 0.5,
          value: 0.5,
        },
      ],
    };
  },
  methods: {
    // 初始化方法
    show() {
      // 显示
      this.visible = true;
      this.loading = false;
      // 重置表单
      this.predictForm = { ...baseForm };
    },
    // 转换数据类型
    toNumberByFiled(data, filed) {
      if (data[filed]) {
        return +data[filed];
      }
      return null;
    },
    // 保存
    predictSave() {
      this.$refs.predictFormRef.validate((valid) => {
        if (valid) {
          const data = { ...this.predictForm };
          //  如果有数据，就转成数字类型
          data.start_point_lon = this.toNumberByFiled(data, "start_point_lon");
          data.start_point_lat = this.toNumberByFiled(data, "start_point_lat");
          data.end_point_lat = this.toNumberByFiled(data, "end_point_lat");
          data.end_point_lon = this.toNumberByFiled(data, "end_point_lon");
          if (!data.end_point_lat) {
            delete data.end_point_lat;
          }
          if (!data.end_point_lon) {
            delete data.end_point_lon;
          }
          if (!data.event_type) {
            delete data.event_type;
          }
          console.log(data);
          this.loading = true;
          request
            .eventAllPredict(data)
            .then((res) => {
              const data = res.data;
              console.log(res);
              if (data.status === "1") {
                this.$message.success(data.message);
                this.visible = false;
                this.$emit("success", res.data);
              }
              if (data.status === "-1") {
                this.$message.error(data.message);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>

</style>